define(function (require, exports, module) {
    var angular = require('angular');
    require("angular-ui");
    require("angular-route");

    var beeParamModule = angular.module('beeParam', [])
        .constant('api.param.path', "/api/index.php?r=xmf/xmf-param/")
        .factory('ParamService', [
            '$http',
            'api.param.path',
            function ($http, api_path) {
                var httpClient = {
                    get: function (api, data) {
                        var url = api_path + api;
                        var search = (data && data.search) || {};
                        angular.forEach(search, function (val, key) {
                            url += "&" + key + "=" + val;
                        });

                        return $http.get(url)
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode === "0") {
                                    return res;
                                }
                            })
                    },
                    post: function (api, data) {
                        return $http.post(api_path + api, (data && data.data) || {})
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode === "0") {
                                    return res;
                                }
                            })
                    }
                }
                return {
                    getParamList: function (data) {
                        return httpClient.post('activity-type-list', data)
                    },
                    createParam: function(data) {
                        return httpClient.post('add-act-type', data)
                    },
                    updateParam: function(data) {
                        return httpClient.post('edit-act-type', data)
                    },
                    deleteParam: function(data) {
                        return httpClient.post('delete-act-type', data)
                    },
                    enableParam: function (data) {
                        return httpClient.post('enable-act-type', data)
                    },
                    disableParam: function (data) {
                        return httpClient.post('disable-act-type', data)
                    },
                    moveupParam: function(data){
                        return httpClient.post('move-up-act', data)
                    },
                    movedownParam: function(data){
                        return httpClient.post('move-down-act', data)
                    },
                    setDefaultParam: function(data) {
                        return httpClient.post('set-act-default', data)
                    },
                    enableSMS: function(data) {
                        return httpClient.post('enable-message', data)
                    },
                    disableSMS: function(data) {
                        return httpClient.post('disable-message', data)
                    },
                    sendSMS: function(data) {
                        return httpClient.post('send-message', data)
                    },
                    getSMSConfig: function(data) {
                        return httpClient.post('get-message-config', data)
                    }
                }
            }
        ])
        .controller('ParamController', [
            '$scope',
            '$rootScope',
            'ParamService',
            function ($scope, $rootScope, service) {

                $scope.getParamList = function(){
                   return service.getParamList({
                        data: {}
                    })
                        .then(function(res){
                            $scope.paramList = res.data;
                        })
                };
                
                $scope.createParam = function(data,dialog){
                    if (!data.type_name) {
                        $rootScope.$emit('error',"选项名称不能为空",true);
                        return false;
                    }
                    if (!data.type_desc) {
                        $rootScope.$emit('error',"选项描述不能为空",true);
                        return false;
                    }
                    return service.createParam({
                        data: {
                            'name': data.type_name,
                            'desc': data.type_desc
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                            dialog.close();
                        })
                };
                
                $scope.updateParam = function(data,dialog){
                    if (!data.type_name) {
                        $rootScope.$emit('error',"选项名称不能为空",true);
                        return false;
                    }
                    if (!data.type_desc) {
                        $rootScope.$emit('error',"选项描述不能为空",true);
                        return false;
                    }
                    return service.updateParam({
                        data: {
                            id: data.type_id,
                            'name': data.type_name,
                            'desc': data.type_desc
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                            dialog.close();
                        })
                };
                
                $scope.deleteParam = function(param){
                    return service.deleteParam({
                        data: {
                            id: param.type_id
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                        })
                };
                
                $scope.enableParam = function(param){
                    return service.enableParam({
                        data: {
                            id: param.type_id
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                        })
                };
                
                $scope.disableParam = function(param){
                    return service.disableParam({
                        data: {
                            id: param.type_id
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                        })
                };
                
                $scope.moveupParam = function(param){
                    return service.moveupParam({
                        data: {
                            id: param.type_id
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                        })
                };
                
                $scope.movedownParam = function(param){
                    return service.movedownParam({
                        data: {
                            id: param.type_id
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                        })
                };
                
                $scope.setDefaultParam = function(param){
                    return service.setDefaultParam({
                        data: {
                            id: param.type_id
                        }
                    })  
                        .then(function(res){
                            $scope.getParamList();
                        })
                };
             
                $scope.sendRegisterSMS = function(){
                    if (!$scope.registerMobileValid) {
                        return;
                    }
                    return service.sendSMS({
                        data: {
                            phone: $scope.register_mobile,
                            organization_id: $scope.org_id,
                            "scene_code":"register"
                        }
                    })  
                        .then(function(res){
                            $rootScope.$emit('success','发送短信成功');
                        })
                };
                
                $scope.sendForgetSMS = function(){
                    if (!$scope.forgetMobileValid) {
                        return;
                    }
                    return service.sendSMS({
                        data: {
                            phone: $scope.forget_mobile,
                            organization_id: $scope.org_id,
                            "scene_code":"forget"
                        }
                    })  
                        .then(function(res){
                            $rootScope.$emit('success','发送短信成功');
                        })
                };
                
                $scope.validRegisterMobile = function() {
                    $scope.registerMobileValid = /^\d{11}$/.test($scope.register_mobile);  
                };
                
                $scope.validForgetMobile = function() {
                    $scope.forgetMobileValid = /^\d{11}$/.test($scope.forget_mobile);  
                };
                
                $scope.getSMSConfig = function(){
                    return service.getSMSConfig({
                        data: {}
                    })
                        .then(function(res){
                            $scope.smsEnable = res.data.message_config == "1";
                            $scope.org_id = res.data.org_id;
                        })
                };
                
                $scope.updateSMSConfig = function() {
                    var api = $scope.smsEnable ? service.enableSMS : service.disableSMS;
                    
                    return api({
                        data: {
                        }
                    })
                        .then(function(res){
                            $rootScope.$emit('success','操作成功');
                        })
                };
                
                $scope.getSMSConfig();
                
                $scope.getParamList();
              
            }
        ])

});
